package com.lyl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyl.domain.SysPrivilege;
import com.lyl.mapper.SysPrivilegeMapper;
import com.lyl.mapper.SysRolePrivilegeMapper;
import com.lyl.service.SysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;


/**
 * @author lylbell
 * @date 2021-06-22 13:31
 */
    
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService {


    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;


    /**
     * 获取所有菜单对应的权限，并判断该角色是否拥有该权限
     * @param menuId
     * @param roleId
     * @return
     */
    @Override
    public List<SysPrivilege> findAllSysPrivilege(Long menuId, Long roleId) {
        //1、通过菜单id，获取所拥有的权限
        List<SysPrivilege> privileges = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId));
        if (CollectionUtils.isEmpty(privileges)) {
            return Collections.emptyList();
        }

        //2、通过角色获取权限，并设置own为1
        Set<Long> privilageIds = sysPrivilegeMapper.findPrivilegeByRoleId(roleId);
        for (SysPrivilege privilege : privileges) {
            if (privilageIds.contains(privilege.getId())) {  //判断该权限是否是该角色拥有的权限
                privilege.setOwn(1);
            }
        }
        return privileges;
    }
}
